<?php

namespace App\Model;


use Illuminate\Support\Facades\DB;

class RoomScore extends BaseModel
{
    protected $table_room_score = "room_score"; //房间积分表
    protected $table_room_score_log = "room_score_log"; //房间增减分日志
    const DEFAULT_REPUTATION = 100; //默认分数

    public function __construct(array $attributes = [])
    {
        parent::__construct();
    }

    /**
     * 根据房间id列表读取房间信誉值
     * @param $roomIds array 房间id列表
     * @return array
     */
    public function getReputationByRoomIds($roomIds)
    {
        $users = DB::connection($this->connection)
            ->table($this->table_room_score)
            ->whereIn('room_id', $roomIds)
            ->get();
        $socres = $this->objToArray($users);
        $socreMap = [];
        if (!empty($socres)) {
            $socreMap = array_column($socres, 'reputation', 'room_id');
        }
        return $socreMap;
    }

    /**
     * 读取一个房间的操作记录
     * @param $roomId int 房间id
     * @param $type int 积分类型 1:房间信誉分
     * @param $page int 分页页数
     * @param $limit int 分页数量
     * @return mixed
     */
    public function getScoreLogByRoomId($roomId, $type, $page, $limit)
    {
        $where['room_id'] = $roomId;
        if (!empty($type)) {
            $where['type'] = $type;
        }
        $lists = DB::connection($this->connection)
            ->table($this->table_room_score_log)
            ->where($where)
            ->orderBy('id', 'DESC')
            ->paginate($limit, ['room_id', 'score', 'desc', 'created_at', 'created_admin'], 'page', $page);
        return $lists;
    }

    public function setRoomReputation($type, $roomId, $score, $desc, $adminName)
    {
        return curl_request(getKkaudioApiUrl() . '/inner/roomScore/setRoomReputation', [
            'room_id' => $roomId,
            'score' => $score,
            'type' => $type,
            'desc' => $desc,
            'adminName' => $adminName,
        ]);
    }

}

